package com.lzx.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lzx.article.entity.Articles;
import com.lzx.article.entity.Comments;
import com.lzx.article.entity.CommentsGiveALike;
import com.lzx.article.feign.UserClient;
import com.lzx.article.mapper.ArticlesMapper;
import com.lzx.article.mapper.CommentsGiveALikeMapper;
import com.lzx.article.mapper.CommentsMapper;
import com.lzx.article.service.CommentsGiveALikeService;
import com.lzx.article.vo.User;
import com.lzx.common.entity.Inform;
import com.lzx.common.enums.InformType;
import com.lzx.common.utils.R;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评论点赞表 服务实现类
 * </p>
 *
 * @author lzx
 * @since 2023-02-01
 */
@Service
public class CommentsGiveALikeServiceImpl extends ServiceImpl<CommentsGiveALikeMapper, CommentsGiveALike> implements CommentsGiveALikeService {
    @Resource
    private CommentsGiveALikeMapper commentsGiveALikeMapper;

    @Resource
    private CommentsMapper commentsMapper;

    @Resource
    private UserClient userClient;

    @Resource
    private ArticlesMapper articlesMapper;

    //评论点赞
    @Transactional
    @Override
    public R giveALikeToComments(CommentsGiveALike commentsGiveALike) {
        QueryWrapper<CommentsGiveALike> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", commentsGiveALike.getArticleId()).eq("parent_id", commentsGiveALike.getParentId()).eq("user_id", commentsGiveALike.getUserId());
        CommentsGiveALike vo = commentsGiveALikeMapper.selectOne(wrapper);
        if (vo != null) {
            if (vo.getIsLike() == 1) {
                vo.setIsLike(0);
                vo.setCreateTime(new Date());
                //更新用户点赞信息
                commentsGiveALikeMapper.updateById(vo);
                //回复的这条记录点赞人数-1
                Comments comments = commentsMapper.selectById(commentsGiveALike.getParentId());
                comments.setLikeCount(comments.getLikeCount() - 1);
                commentsMapper.updateById(comments);
                return R.ok().message("取消点赞");
            } else {
                vo.setIsLike(1);
                vo.setCreateTime(new Date());
                //更新用户点赞信息
                commentsGiveALikeMapper.updateById(vo);
                //回复的这条记录点赞人数+1
                Comments comments = commentsMapper.selectById(commentsGiveALike.getParentId());
                comments.setLikeCount(comments.getLikeCount() + 1);
                commentsMapper.updateById(comments);
                return R.ok().message("点赞成功");
            }
        } else {
            //保存用户点赞信息
            commentsGiveALike.setIsLike(1);
            commentsGiveALike.setCreateTime(new Date());
            commentsGiveALikeMapper.insert(commentsGiveALike);
            //回复的这条记录点赞人数+1
            Comments comments = commentsMapper.selectById(commentsGiveALike.getParentId());
            comments.setLikeCount(comments.getLikeCount() + 1);
            commentsMapper.updateById(comments);
            //回复的用户积分+1
            userClient.likeProblem(Math.toIntExact(comments.getUserId()));

            //发送通知
            Inform inform = new Inform();
            inform.setUserId(Math.toIntExact(comments.getUserId()));
            User user = userClient.getUserinfoByIdToOtherServe(Long.valueOf(commentsGiveALike.getUserId()));
            inform.setSender(commentsGiveALike.getUserId());
            inform.setNickname(user.getNickname());
            inform.setAvatar(user.getAvatar());
            inform.setType(InformType.LIKE.getType());
            Articles articles = articlesMapper.selectById(commentsGiveALike.getArticleId());
            inform.setMessage(articles.getTitle());
            userClient.saveInform(inform);

            return R.ok().message("点赞成功");
        }
    }

    //根据文章id和用户id查询用户点赞信息
    @Override
    public R selectCommentsGiveALive(Long articleId, Long userId) {
        QueryWrapper<CommentsGiveALike> wrapper = new QueryWrapper<>();
        wrapper.eq("article_id", articleId).eq("user_id", userId);
        List<CommentsGiveALike> list = commentsGiveALikeMapper.selectList(wrapper);
        return R.ok().data("list", list);
    }
}
